package com.fadis.data;


import com.fadis.data.base.FadisString;
import com.fadis.utils.FadisUtils;

public class FString {

    public static String append(String src, String append){
        return src + append;
    }

    public static String setrange(String s, int offset, String replaceStr){
        int originStart = 0;
        int originEnd = s.length() - 1;
        int start = (int) offset;
        int end = start + replaceStr.length() - 1;
        if(start > originEnd){
            int delta = start - originEnd - 1;
            for (int i = 0; i < delta; i++) {
                replaceStr = " " + replaceStr;
            }
            start -= delta;
            end = start + replaceStr.length() - 1;
        }
        if(end > originEnd){
            int delta = end - originEnd;
            for (int i = 0; i < delta; i++) {
                s += " ";
            }
            originEnd += delta;
        }

        String newS = "";
        for (int i = 0; i < s.length(); i++) {
            if(i >= start && i <= end){
                newS += replaceStr.charAt(i - start);
            }else{
                newS += s.charAt(i);
            }
        }
        return newS;
    }

    public static String getrange(String src, long startOffset, long endOffset) {
        if(src == null) return null;
        if(src.equals("")) return "";
        int start = FadisUtils.getNormalIndex((int) startOffset, src.length());
        int end = FadisUtils.getNormalIndex((int) endOffset, src.length());

        if(start >= src.length()) return "";
        if(end >= src.length()) end = src.length() - 1;
        if(start==end) return src.charAt(start)+"";
        if(start > end) return "";

        String s2 = "";
        for (int i = start; i <= end; i++) {
            s2 += src.charAt(i);
        }
        return s2;
    }

    public static int strlen(FadisString s){
        return s == null || s.getStr() == null ? 0 : s.getStr().length();
    }


}
